package cn.aws360.boot.system.starter.controller;

import cn.aws360.boot.base.api.annotation.OpsLog;
import cn.aws360.boot.base.api.constant.enums.OpsTypeEnum;
import cn.aws360.boot.base.api.pojo.response.ResultModel;
import cn.aws360.boot.base.api.utils.ServletUtils;
import cn.aws360.boot.base.api.validation.ValidationGroup;
import cn.aws360.boot.data.api.pojo.request.PageQuery;
import cn.aws360.boot.data.api.pojo.response.PageResult;
import cn.aws360.boot.system.api.pojo.request.SysConfQuery;
import cn.aws360.boot.system.api.pojo.request.SysConfWrite;
import cn.aws360.boot.system.api.pojo.response.SysConfModel;
import cn.aws360.boot.system.biz.service.SysConfService;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.aws360.boot.system.api.constant.SysConstant.*;

/**
 * @author junnan
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/conf")
@ApiSupport(order = MODULE_CONF_CODE)
@Api(value = MODULE_CONF_NAME, tags = MODULE_CONF_TAGS)
public class SysConfController {

    private final SysConfService confService;

    @GetMapping("/index.json")
    @SaCheckPermission("sys:conf:query")
    @ApiOperation(value = "参配页面", hidden = true)
    public ResultModel<JSONObject> index() {
        return ResultModel.ok(ServletUtils.getPageJson());
    }

    @GetMapping("/list")
    @SaCheckPermission("sys:conf:query")
    @ApiOperation(value = "参配列表")
    @ApiOperationSupport(order = 1)
    public ResultModel<List<SysConfModel>> list(SysConfQuery sysConfQuery) {
        return ResultModel.ok(confService.findList(sysConfQuery));
    }

    @GetMapping("/page")
    @SaCheckPermission("sys:conf:query")
    @ApiOperation(value = "参配分页")
    @ApiOperationSupport(order = 1)
    public ResultModel<PageResult<SysConfModel>> page(SysConfQuery sysConfQuery, PageQuery pageQuery) {
        return ResultModel.ok(confService.findPage(sysConfQuery, pageQuery));
    }

    @GetMapping("/{id}")
    @SaCheckPermission("sys:conf:detail")
    @ApiOperation(value = "参配详情")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParam(name = "id", value = "参配 id", dataTypeClass = Integer.class, required = true)
    public ResultModel<SysConfModel> detail(@PathVariable Integer id) {
        return ResultModel.ok(confService.detail(id));
    }

    @PostMapping
    @SaCheckPermission("sys:conf:add")
    @OpsLog(title = "新增参配", opsType = OpsTypeEnum.INSERT)
    @ApiOperation(value = "新增参配")
    @ApiOperationSupport(order = 2)
    public ResultModel<Boolean> add(@Validated(ValidationGroup.add.class) @RequestBody SysConfWrite sysConfWrite) {
        return ResultModel.ok(confService.add(sysConfWrite));
    }

    @PutMapping()
    @SaCheckPermission("sys:conf:edit")
    @OpsLog(title = "修改参配", opsType = OpsTypeEnum.UPDATE)
    @ApiOperation(value = "修改参配")
    @ApiOperationSupport(order = 3)
    public ResultModel<Boolean> edit(@Validated(ValidationGroup.edit.class) @RequestBody SysConfWrite sysConfWrite) {
        return ResultModel.ok(confService.edit(sysConfWrite));
    }

    @DeleteMapping("/{ids}")
    @SaCheckPermission("sys:conf:remove")
    @OpsLog(title = "移除参配", opsType = OpsTypeEnum.REMOVE)
    @ApiOperation(value = "移除参配")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParam(name = "ids", value = "参配 ids", dataTypeClass = String.class, required = true)
    public ResultModel<Boolean> remove(@PathVariable String ids) {
        return ResultModel.ok(confService.remove(ids));
    }

    @GetMapping("/refresh")
    @SaCheckPermission("sys:conf:query")
    @ApiOperation(value = "参配缓存刷新")
    @ApiOperationSupport(order = 20)
    public ResultModel<Boolean> refresh() {
        return ResultModel.ok(confService.refreshCache());
    }

    @GetMapping("/code/{code}")
    @ApiOperation(value = "根据参配编码获取键值")
    @ApiOperationSupport(order = 21)
    @ApiImplicitParam(name = "code", value = "参配 code", dataTypeClass = String.class, required = true)
    public ResultModel<String> getValueByCode(@PathVariable String code) {
        return ResultModel.ok(confService.getValueByCode(code));
    }

}
